<?php
class User extends AppModel {
	public $name = 'User';
	public $validate = array(
			'user_name' => array(
					'alphaNumberic'=> array(
						'rule'     => 'alphaNumeric',
						'required'   => true,
						'message'    => 'The User Name only accept alphabet or numberic characters.'
					),
					'between' => array(
		                'rule'    => array('between', 5, 32),
		                'message' => 'Between 5 to 32 characters'
		            ),
					'exists' => array(
						'rule'=>'doesNotExists',
						'message'=>'The User Name was in used.'
					)
			),
			'email' => 'email',
			'password' => array(
					'rule'    => array('between', 6, 32),
					'required'   => true,
					'message' => 'Passwords must be between 6 and 32 characters long.'
			)
	);
	
	
	public function doesNotExists($check){
		// $check will have value: array('user_name' => 'some-value')
		$existsUsers = $this->find('all', array('conditions'=> array('user_name' => $check['user_name'])));
		if(count($existsUsers) > 0){
			return false;
		}
		return true;
	}
	
	public function beforeSave(){
		if (!empty($this->data['User']['password'])){
			$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
			return true;
		}
	}
}